import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Req,
} from '@nestjs/common';
import { ShareService } from './share.service';
import { CreateShareDto } from './dto/create-share.dto';
import { Request } from 'express';
import { UpdateShareDto } from './dto/update-share.dto';
import { CreateUserToShareDto } from './dto/create-usertoshare.dto';
import { CreateGroupToShareDto } from './dto/create-grouptoshare.dto';
import { Public } from 'src/common/decorator/public.decorator';

@Controller('share')
export class ShareController {
  constructor(private readonly shareService: ShareService) {}


  @Post('createShare')
  createShare(@Body() createShareDto: CreateShareDto, @Req() request: Request) {
    const user = request.user;
    createShareDto.belong = (user as any).email;
    return this.shareService.createShare(createShareDto);
  }

  @Post('createUserToShare')
  createUserToShare(@Body() createUserToShareDto:CreateUserToShareDto, @Req() request: Request){
    const user = request.user;
    createUserToShareDto.useruuid = (user as any).id
    return this.shareService.createUserToShare(createUserToShareDto);
  }

  @Get('getAllShares')
  getAllShares() {
    return this.shareService.getAllShares();
  }

  @Public()
  @Get('getShareItemById/:id')
  getShareItemById(@Param('id') id: string) {
    return this.shareService.getShareItemById(id);
  }

  @Public()
  @Get('checkShareById/:id')
  checkShareById(@Param('id') id: string) {
    return this.shareService.checkShareById(id);
  }

  @Public()
  @Get('checkShareByScenceuuid/:scenceuuid')
  checkShareByScenceuuid(@Param('scenceuuid') scenceuuid:string){
    return this.shareService.checkShareByScenceuuid(scenceuuid)
  }
  
  @Get('checkOnceReceiveShareByShareuuid/:shareuuid')
  checkOnceReceiveShareByShareuuid(@Param('shareuuid') shareuuid:string, @Req() request: Request){
    const user = request.user;
    return this.shareService.checkOnceReceiveShareByShareuuid(shareuuid,(user as any).id)
  }

  @Get('getSendShareItemByUser')
  getSendShareItemByUser(@Req() request: Request) {
    const user = request.user;
    const email = (user as any).email;
    return this.shareService.getSendShareItemByUser(email);
  }

  @Get('getReceiveShareItemByUser')
  getReceiveShareItemByUser(@Req() request: Request) {
    const user = request.user;
    const email = (user as any).email;
    return this.shareService.getReceiveShareItemByUser(email);
  }

  @Patch('updateShareItemById/:id')
  updateShareItemById(
    @Param('id') id: string,
    @Body() updateShareDto: UpdateShareDto,
  ) {
    return this.shareService.updateShareItemById(id, updateShareDto);
  }

  @Delete('removeShareItemById/:id')
  removeShareItemById(@Param('id') id: string) {
    return this.shareService.removeShareItemById(id);
  }

  @Delete('removeUserToShareItemByShareId/:id')
  removeUserToShareItemByShareId(@Param('id') id: string, @Req() request: Request) {
    const user = request.user;
    return this.shareService.removeUserToShareItemByShareId(id,(user as any).id);
  }

  // 小组相关接口
  @Post('createShareByGroup')
  createShareByGroup(@Body() createShareDto: CreateShareDto,@Req() request: Request){
    const user = request.user;
    return this.shareService.createShareByGroup(createShareDto,(user as any).email);
  }

  @Post('createGroupToShare')
  createGroupToShare(@Body() createGroupToShareDto:CreateGroupToShareDto, @Req() request: Request){
    const user = request.user;
    createGroupToShareDto.sharer = (user as any).email;
    return this.shareService.createGroupToShare(createGroupToShareDto);
  }

  @Post('checkOnceReceiveShareGroupByShareuuid')
  checkOnceReceiveShareGroupByShareuuid(@Body() checkJsonData: any, @Req() request: Request) {
    const user = request.user;
    return this.shareService.checkOnceReceiveShareGroupByShareuuid(checkJsonData,(user as any).id)
  }

  @Get('getSendShareItemByGroup/:groupuuid')
  getSendShareItemByGroup(@Param('groupuuid') groupuuid: string){
    return this.shareService.getSendShareItemByGroup(groupuuid);
  }

  @Get('getReceiveShareItemByGroup/:groupuuid')
  getReceiveShareItemByGroup(@Param('groupuuid') groupuuid: string){
    return this.shareService.getReceiveShareItemByGroup(groupuuid);
  }

  @Delete('removeShareItemOfGroupById/:id')
  removeShareItemOfGroupById(@Param('id') id: string){
    return this.shareService.removeShareItemOfGroupById(id);
  }

  @Delete('removeGroupToShareItemByShareId')
  removeGroupToShareItemByShareId(@Body() removeData:any){
    return this.shareService.removeGroupToShareItemByShareId(removeData);
  }
}
